package com.hfzy.ihk.common.core.freeswitch.robot;

import org.freeswitch.DTMFCallback;
import org.freeswitch.FreeswitchScript;
import org.freeswitch.HangupHook;
import org.freeswitch.StateHandler;
import org.freeswitch.swig.JavaSession;
import org.freeswitch.swig.freeswitch;

/**
 * Created by Administrator on 2018/10/19.
 *
 * @author wws
 */
public class FreeswitchPhoneTest implements FreeswitchScript, DTMFCallback, HangupHook,StateHandler.OnInitHandler,
        StateHandler.OnDestroyHandler,StateHandler.OnConsumeMediaHandler,StateHandler.OnExecuteHandler,StateHandler.OnHangupHandler,
        StateHandler.OnRoutingHandler,StateHandler.OnSoftExecuteHandler,StateHandler.OnExchangeMediaHandler





{
    public FreeswitchPhoneTest()
    {
    }

    @Override
    public String onDTMF(Object object, int i, String arg)
    {
        if (object instanceof String) {
            freeswitch.console_log("notice", "DTMF: " + (String) object + " ARG: " + arg + "\n");
        } else {
            freeswitch.console_log("notice", "WOW GOT AN EVENT: " + object.toString());
        }
        return "true";
    }

    @Override
    public void onHangup()
    {
        freeswitch.console_log("notice", "FreeswitchPhoneTest wws mod java test--->HANGUP!\n");
    }

    @Override
    public int onConsumeMedia(String s) {
        freeswitch.console_log("notice", "FreeswitchPhoneTest wws mod java test--->onConsumeMedia: " + s +"\n");
        return 0;
    }

    @Override
    public int onDestroy(String s) {
        freeswitch.console_log("notice", "FreeswitchPhoneTest wws mod java test--->onDestroy: " + s +"\n");
        return 0;
    }

    @Override
    public int onExchangeMedia(String s) {
        freeswitch.console_log("notice", "FreeswitchPhoneTest wws mod java test--->onExchangeMedia: " + s +"\n");
        return 0;
    }

    @Override
    public int onExecute(String s) {
        freeswitch.console_log("notice", "FreeswitchPhoneTest wws mod java test--->onExecute: " + s +"\n");
        return 0;
    }

    @Override
    public int onHangup(String s, String s1) {
        freeswitch.console_log("notice", "FreeswitchPhoneTest wws mod java test--->onHangup: " + s +"\n");
        return 0;
    }

    @Override
    public int onInit(String s) {
        freeswitch.console_log("notice", "FreeswitchPhoneTest wws mod java test--->onInit: " + s +"\n");
        return 0;
    }

    @Override
    public int onRouting(String s) {
        freeswitch.console_log("notice", "FreeswitchPhoneTest wws mod java test--->onRouting: " + s +"\n");
        return 0;
    }

    @Override
    public int onSoftExecute(String s) {
        freeswitch.console_log("notice", "FreeswitchPhoneTest wws mod java test--->onSoftExecute: " + s +"\n");
        return 0;
    }

    @Override
    public void run(String sessionUuid, String args)
    {
        freeswitch.console_log("notice", "UUID: " + sessionUuid + " ARGS: " + args + "\n");
        JavaSession session = null;
        try{
            session = new JavaSession(sessionUuid);
            session.setDTMFCallback(this, "TEST");
            session.setHangupHook(this);
//session.execute();
            session.answer();
            session.streamFile(args, 0);
            session.hangup("FreeswitchPhoneTest wws挂机");
        }finally{
            if (session != null) {
                session.delete();
            }
        }
    }

}